Imports Microsoft.VisualBasic
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System
Imports System.Data

Imports GTSDAL
Imports GTSDALTableAdapters
Imports GTSSystem.BLL.Location
Imports GTSSystem.BLL.ContactInfo
Imports GTSSystem.BLL.IdGenerator
Namespace GTSSystem.BLL
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks>
    ''' TODO: 
    ''' LookupShipmentsbyClientID
    ''' DeleteShipment
    ''' </remarks>
    <System.ComponentModel.DataObject()> _
     Public Class Shipment
        Private _ShipmentAdaptor As ShipmentTableAdapter = Nothing
        Protected ReadOnly Property ShipmentAdaptor() As ShipmentTableAdapter
            Get
                If _ShipmentAdaptor Is Nothing Then
                    _ShipmentAdaptor = New ShipmentTableAdapter
                End If
                Return _ShipmentAdaptor
            End Get
        End Property
        Public Sub New()
            'Constructor Code here
        End Sub

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Insert, True)> _
        Public Function CreateShipment(ByVal Shipmentdate As Date, _
                                        ByVal ClientId As Integer, _
                                        ByVal ClientBranchid As Integer, _
                                        ByVal CarrierId As Integer, _
                                        ByVal Weight As Double, _
                                        ByVal Origin As Integer, _
                                        ByVal OriginAddress As String, _
                                        ByVal OriginPostalCode As String, _
                                        ByVal Destination As Integer, _
                                        ByVal DestinationAddress As String, _
                                        ByVal DestinationPostalCode As String, _
                                        ByVal ServicetTypeId As Integer, _
                                        ByVal Description As String, _
                                        ByVal GST As Double, _
                                        ByVal PST As Double, _
                                        ByVal HST As Double, _
                                        ByVal Surcharge As Double, _
                                        ByVal TotalPieces As Integer, _
                                        ByVal UserName As String, _
                                        ByVal StatusId As Integer, _
                                        ByVal ShipmentNumber As Integer, _
                                        ByVal Total As Double, _
                                        ByVal SurchargeAmount As Double) As Integer
            Dim ShipmentPK As New Integer
            ShipmentPK = GetNextId("Shipment")

            Try
                CreateShipment = ShipmentAdaptor.Insert(ShipmentPK, UserName, Shipmentdate, StatusId, ClientBranchid, ClientId, CarrierId, Weight, ServicetTypeId, Description, GST, PST, HST, Surcharge, TotalPieces, ShipmentNumber, OriginAddress, OriginPostalCode, DestinationAddress, DestinationPostalCode, Origin, Destination, Total, SurchargeAmount)
            Catch ex As Exception
                Throw
            End Try

        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Update, True)> _
        Public Function UpdateShipment(ByVal ReferenceNumber As Integer, _
                                              ByVal Shipmentdate As Date, _
                                              ByVal ClientId As Integer, _
                                              ByVal ClientBranchid As Integer, _
                                              ByVal CarrierId As Integer, _
                                              ByVal Weight As Double, _
                                              ByVal OriginID As Integer, _
                                              ByVal OriginAddress As String, _
                                              ByVal OriginPostalCode As String, _
                                              ByVal DestinationID As Integer, _
                                              ByVal DestinationAddress As String, _
                                              ByVal DestinationPostalCode As String, _
                                              ByVal ServiceTypeId As Integer, _
                                              ByVal Description As String, _
                                              ByVal GST As Double, _
                                              ByVal PST As Double, _
                                              ByVal HST As Double, _
                                              ByVal Surcharge As Double, _
                                              ByVal NrOfPieces As Integer, _
                                              ByVal UserName As String, _
                                              ByVal StatusId As Integer, _
                                              ByVal ShipmentNumber As Integer, _
                                              ByVal Total As Double, _
                                              ByVal SurchargeAmount As Double) As Integer

            Try
                UpdateShipment = ShipmentAdaptor.Update(UserName, Shipmentdate, StatusId, ClientBranchid, ClientId, CarrierId, Weight, ServiceTypeId, Description, GST, PST, HST, Surcharge, NrOfPieces, ShipmentNumber, OriginAddress, OriginPostalCode, DestinationAddress, DestinationPostalCode, OriginID, DestinationID, Total, SurchargeAmount, ReferenceNumber)

            Catch ex As Exception
                Throw
            End Try

        End Function

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Update, True)> _
              Public Function UpdateItemsandWeightShipment(ByVal ReferenceNumber As Integer, ByVal TotalItems As Integer, ByVal TotalWeight As Double) As Integer
            Try
                UpdateItemsandWeightShipment = ShipmentAdaptor.UpdateItemsandWeight(TotalWeight, TotalItems, ReferenceNumber)
            Catch ex As Exception
                Throw
            End Try

        End Function

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
        Public Function getAllShipmentsByBranchID(ByVal BranchID As Integer) As ShipmentDataTable
            Try
                Dim user As MembershipUser = Membership.GetUser
                If (Roles.IsUserInRole(user.UserName, "GTSAdministrator")) Then
                    getAllShipmentsByBranchID = ShipmentAdaptor.GetShipments
                Else
                    getAllShipmentsByBranchID = ShipmentAdaptor.GetShipmentsByBranchID(BranchID)
                End If

            Catch ex As Exception
                Throw
            End Try
        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
       Public Function getAllShipmentsByClientID(ByVal ClientID As Integer) As ShipmentDataTable
            Try
                getAllShipmentsByClientID = ShipmentAdaptor.GetShipmentsByClientID(ClientID)
            Catch ex As Exception
                Throw
            End Try
        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
                Public Function getAllActiveShipments(ByVal BranchID As Integer) As ShipmentDataTable
            Try
                getAllActiveShipments = ShipmentAdaptor.GetActiveShipments(BranchID)
            Catch ex As Exception
                Throw
            End Try
        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, False)> _
               Public Function SearchShipments(ByVal BranchID As Integer, _
                                                ByVal ShipmentNumber As String, _
                                                ByVal BeginDate As Date, _
                                                ByVal EndDate As Date, _
                                                ByVal Description As String) _
                                                As ShipmentDataTable
            Try
                SearchShipments = ShipmentAdaptor.SearchShipments(ShipmentNumber, EndDate, BeginDate, Description, BranchID)
            Catch ex As Exception
                Throw
            End Try
        End Function


        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
        Public Function getShipmentByReferencenumber(ByVal ReferenceNumber As Integer) As ShipmentDataTable
            Try
                getShipmentByReferencenumber = ShipmentAdaptor.GetShipmentByReferenceNumber(ReferenceNumber)
            Catch ex As Exception
                Throw
            End Try
        End Function
    End Class
End Namespace
